package com.ruiyi.careryun.web.ota;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ruiyi.careryun.beans.common.ResultMsg;
import com.ruiyi.careryun.service.ota.OtaService;
import com.ruiyi.careryun.service.ota.ServerConfigInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = "升级信息接口")
@Controller
@RequestMapping("/ota")
public class OtaController {

    @Autowired
    private OtaService otaService;
    @Autowired
    private ServerConfigInfoService serverConfigInfoService;

    @ApiIgnore
    @RequestMapping("/toupdate")
    public String index() {
        return "/ota/upload";
    }

    @ApiOperation(value = "版本更新", notes = "")
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg<Map<String, Object>> otaFileUpload(@ApiParam(name = "version", value = "版本号") @RequestParam(value = "version", required = true) String newVersion,
                                                        @ApiParam(name = "file", value = "升级文件") MultipartFile file, HttpServletRequest request)
        throws Exception {
        if (newVersion == null || newVersion.equals("")) {
            return ResultMsg.error("版本号未填写...");
        }
        if (file.getSize() == 0) {
            return ResultMsg.error("文件未选择...");
        }
        // 获取旧版本号
        String oldVersion = serverConfigInfoService.getConfigValueByConfigName("linuxNewVersion");
        return otaService.updatePackage(newVersion, file, request, oldVersion);
    }
}
